//
//  AppDelegate.m
//  Arts1
//
//  Created by apple on 15/8/28.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "HomeListViewController.h"
#import <RongIMKit/RongIMKit.h>
#import "WXApi.h"

#define RONGCLOUD_IM_APPKEY @"sfci50a7cp5xi" // online key sfci50a7cp5xi
#define RONGCLOUD_IM_TOKEN @"sSgtLoIkT4SLkLxO7XDlFns3HPXmliWr+FZvlNNnR1pYGQlkOyCchyJxZ6B7S31jP9Y0imN/IJ09H9YsQACQKw=="

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.layer.cornerRadius = 5;
    self.window.layer.masksToBounds = YES;
    self.window.rootViewController = [[RootViewController alloc] init];
    [self.window makeKeyAndVisible];
    
    //初始化融云SDK。
    [[RCIM sharedRCIM] initWithAppKey:RONGCLOUD_IM_APPKEY];
    
    [[RCIM sharedRCIM] connectWithToken:RONGCLOUD_IM_TOKEN success:^(NSString *userId) {
        
        NSLog(@"userId=============%@",userId);
        
    } error:^(RCConnectErrorCode status) {
        
    } tokenIncorrect:^{
        
    }];
    
    
    /**
     * 推送处理1
     */
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:settings];
    } else {
        // 注册苹果推送，申请推送权限。
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
    }
    
    
    // 微信登陆注册
    [WXApi registerApp:@"wx35ed95caedc388d2"];
    
    
    //  取消自身登陆系统记录
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"dataForKeyAndUid"];
    // 取消微信第三方登陆记录
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"weixinLogin"];
    
    
    
    // 友盟分享注册
 //   [UMSocialData setAppKey:@"5609e1fde0f55a76b9005275"];
    //设置微信AppId、appSecret，分享url
//    [UMSocialWechatHandler setWXAppId:@"wx35ed95caedc388d2" appSecret:@"eb8e6d76ed71c1ab30a44d84ca114655" url:@"http://www.umeng.com/social"];
    
    return YES;
    
    
}

#pragma mark - 即时通讯推送
/**
 * 推送处理2 点击是，获取苹果推送成功
 */
//注册用户通知设置
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings
{
    // register to receive notifications
    [application registerForRemoteNotifications];
}

/**
 * 推送处理3 点击否的执行的方法
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""]
    stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" "
    withString:@""];
    
    [[RCIMClient sharedRCIMClient] setDeviceToken:token];
}

#pragma mark --- 微信登陆回调方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    //  微信登陆回调方法
    BOOL result = [WXApi handleOpenURL:url delegate:self];
        return result;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //  微信登陆回调方法
    BOOL result = [WXApi handleOpenURL:url delegate:self];
        return result;

}

- (void)onResp:(BaseResp *)resp
{
    /*
     ErrCode ERR_OK = 0(用户同意)
     ERR_AUTH_DENIED = -4（用户拒绝授权）
     ERR_USER_CANCEL = -2（用户取消）
     code    用户换取access_token的code，仅在ErrCode为0时有效
     state   第三方程序发送时用来标识其请求的唯一性的标志，由第三方程序调用sendReq时传入，由微信终端回传，state字符串长度不能超过1K
     lang    微信客户端当前语言
     country 微信用户当前国家信息
     */
    SendAuthResp *aresp = (SendAuthResp *)resp;
    if (aresp.errCode == 0)
    {
     NSString *weixinLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"weixinLogin"];
        if (weixinLogin) {
            NSString *code = aresp.code;
            // 微信登陆成功后，执行该方法用来进一步获得用户信息
            if (_delegate && [_delegate respondsToSelector:@selector(getUserInformationWithCode:)]) {
                [_delegate getUserInformationWithCode:code];
            }
        }
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end